import {useEditorStore} from '@/stores/editStore';
import {replaceSelectedText} from '@/utils/tiptap/replaceText';
import http from '@/utils/http';
import {JsonToMarkdown} from '@/utils/JsonToMarkDown';
import {storeToRefs} from 'pinia';
import api from '@/api';

export const aipolish = () => {
  const editorStore = useEditorStore()
  const {editorInstance} = storeToRefs(editorStore)
  const {from, to} = editorInstance.value.state.selection
  const selectMarkDown = JsonToMarkdown(editorInstance.value?.state.doc.cut(from, to).toJSON())
  api.editorAi.polish({content: selectMarkDown}).then(res => {
    if (res.code == 200) {
      replaceSelectedText(editorStore.editorInstance, from, to, res.data)
    } else {
      alert("润色失败忘了错误")
    }

  })
}
